﻿
<%@ Page language="vb" Inherits="PubProject.value" CodeBehind="value.aspx.vb" AutoEventWireup="false" %>
<html>
<head><META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8"/>
<title><%= title %></title>
<link rel="stylesheet" type="text/css" href="/_common/styles/global.css">
<link rel="stylesheet" type="text/css" href="/_common/styles/select.css">
<link rel="stylesheet" type="text/css" href="/_common/styles/dialogs.css">
<script language="javascript" src="/_common/scripts/global.js"></script>
<script language="javascript" src="/Tools/EmailTemplateEditor/Scripts/datafields.js"></script>
<script language="javascript" src="/Tools/EmailTemplateEditor/Scripts/objects.js"></script>
<script language="JavaScript">

	var _oXml;

	var _sEntLogName	= null;
	var _sEntDispName	= null;
	var _sAttrLogName	= null;
	var _sAttrDispName	= null;
	var _sDataType		= null;


	function window.onload()
	{
		with (window.dialogArguments)
		{
			_oXml			= Xml;
			_sEntLogName	= Ent;
			_sAttrLogName	= Attr;
		}

		BuildEntity();

		BuildAttribute(_sEntLogName);
	}
	
	
	function document.onkeydown()
	{
		if (event.srcElement.tagName != "BUTTON" && event.keyCode == 13)
		{
			event.returnValue = false;

			Ok();
		}
		else if (event.keyCode == 116)
		{
			event.keyCode =0;
			event.returnValue = false;	
		}
	}


	function Ok()
	{
		window.returnValue = new ValueObj(_sEntLogName, _sEntDispName, _sAttrLogName, _sAttrDispName, _sDataType);
		window.close();
	}


	function BuildEntity()
	{
		var oEntities = _oXml.selectNodes("/entities/entity");

		var sHtml = "<span class='selectBox' value='" + _sEntLogName + "' name='ObjectTypeCode' changeHandler='UpdateAttribute'><table cellspacing='0' cellpadding='2' style='display: none;'>";

		var i;
		var iLen = oEntities.length;

		for (i = 0; i < iLen; i++)
		{
			sHtml += "<tr><td val='" + oEntities[i].getAttribute("name") + "'>" + oEntities[i].getAttribute("desc") + "</td></tr>";
		}						

		sHtml +="</table></span>";

		Entity.innerHTML = sHtml;

		_sEntDispName = _oXml.selectSingleNode("/entities/entity[@name = '" + _sEntLogName + "']").getAttribute("desc");
	}


	function BuildAttribute(s)
	{
		if (_sAttrLogName)
		{
			Attribute.innerHTML = "<span class='selectBox' value='" + _sAttrLogName + "," + _oXml.selectSingleNode("/entities/entity[@name = '" + s + "']/fields/field[@name = '" + _sAttrLogName + "']").getAttribute("datatype") + "' changeHandler='onFieldChange'>" + _oXml.selectSingleNode("/entities/entity[@name = '" + s + "']/div/span/table").xml + "</span>";
		}
		else
		{
			Attribute.innerHTML = _oXml.selectSingleNode("/entities/entity[@name = '" + s + "']/div/span").xml;
		}

		toggleBtnDialogOk();
	}


	// this is the onchange event handler for the entity picklist
	//
	function UpdateAttribute(o)
	{
		_sEntLogName	= o.returnValue
		_sEntDispName	= o.value

		_sAttrLogName	= null;
		_sAttrDispName	= null;
		_sDataType		= null;

		BuildAttribute(_sEntLogName);
	}


	// this is the onchange event handler for the attribute picklist
	//
	function onFieldChange(o)
	{
		var a = o.returnValue.split(",");

		_sAttrLogName	= a[0];
		_sAttrDispName	= o.value;
		_sDataType		= a[1];

		toggleBtnDialogOk();
	}


	function toggleBtnDialogOk()
	{
		crmDialog.btnDialogOk.disabled = (!_sDataType);
	}

</script>
</head>
<body>
<form name="crmDialog">
	<table border="0" cellspacing="0" cellpadding="8" width="100%" height="100%">
		<tr>
			<td class="header">
				<div class="header"><%= title %></div>
				<div class="headerdesc"><%= description %></div>
			</td>
		</tr>
		<tr>
			<td class="main">
				<table cellpadding="0" cellspacing="5" width="100%" style="table-layout: fixed;">
					<col width="50"><col>
					<tr>
						<td>Record Type</td>
						<td id="Entity"></td>
					</tr>
					<tr>
						<td>Field</td>
						<td id="Attribute"></td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td class="buttons">
				<button id="btnDialogOk" onclick="Ok();">OK</button>
				<span>&nbsp;</span>
				<button id="btnDialogCancel" onclick="window.close();">Cancel</button>
			</td>
		</tr>
	</table>
</form>
</body>
</html>
